/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.IO;
using NUnit.Framework;

namespace OpenServers.Net.Common.Utils {
    [TestFixture]
    public class CompositeStreamTest {
        private CompositeStream _stream;

        [SetUp]
        public void SetUp() {
            _stream = new CompositeStream();
        }

        [Test]
        public void TestSeek() {
            try {
                _stream.Seek(0, SeekOrigin.Begin);
                Assert.Fail("should fail");
            } catch (NotImplementedException) {
                // ok
            }
        }

        [Test]
        public void TestSetLength() {
            try {
                _stream.SetLength(100);
                Assert.Fail("should fail");
            } catch (NotImplementedException) {
                // ok
            }
        }

        [Test]
        public void TestWrite() {
            try {
                _stream.Write(new byte[] { 0, 1, 2 }, 0, 3);
                Assert.Fail("should fail");
            } catch (NotImplementedException) {
                // ok
            }
        }

        [Test]
        public void TestSetPosition() {
            try {
                _stream.Position = 1;
                Assert.Fail("should fail");
            } catch (NotImplementedException) {
                // ok
            }
        }

        [Test]
        public void TestFlush() {
            try {
                _stream.Flush();
                Assert.Fail("should fail");
            } catch (NotImplementedException) {
                // ok
            }
        }

        [Test]
        public void TestCanRead() {
            Assert.IsTrue(_stream.CanRead);
        }

        [Test]
        public void TestCanWrite() {
            Assert.IsFalse(_stream.CanWrite);
        }

        [Test]
        public void TestCanSeek() {
            Assert.IsFalse(_stream.CanSeek);
        }

        [Test]
        public void TestRead() {
            MemoryStream memoryStream1 = new MemoryStream(new byte[] { 0, 1, 2 });
            MemoryStream memoryStream2 = new MemoryStream(new byte[] { 3, 4, 5 });

            _stream.Add(memoryStream1);
            _stream.Add(memoryStream2);

            Assert.AreEqual(0, _stream.Position);
            Assert.AreEqual(6, _stream.Length);

            byte[] buffer = new byte[10];
            int read = _stream.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(6, read);
            Assert.IsTrue(ArrayUtils.Compare(new byte[] { 0, 1, 2, 3, 4, 5 }, buffer, 6));

            Assert.AreEqual(6, _stream.Position);
            Assert.AreEqual(0, _stream.Length);
        }
    }
}
